package com.wwdx.controller.admin;

import com.wwdx.controller.base.BaseController;
import com.wwdx.model.WwdxBannerModel;
import com.wwdx.utils.ConstantHolder;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.Map;

/**
 * @author liguoliang
 * @date  2017/12/2.
 */
@Controller
@RequestMapping("/system/wwdx_banner")
public class WwdxBannerController extends BaseController{

    /**
     * 分页查询
     */
    @RequestMapping("/list")
    public ModelAndView index(WwdxBannerModel model,
                              @RequestParam(value = "page", defaultValue = "1") Integer page,
                              @RequestParam(value = "rows",defaultValue = "10") Integer rows) {

        ModelAndView mav = new ModelAndView();
        mav.addObject("listSize", wwdxBannerService.selectModelCount(model));
        mav.addObject("list", wwdxBannerService.selectModelList(model,page, rows));
        return mav;
    }
    
    /**
     * 新增
     */
    @RequestMapping("/create")
    public ModelAndView create(WwdxBannerModel model) {
        ModelAndView mv = new ModelAndView();
        mv.addObject("model",model);
        return mv;
    }

    /**
     * 保存新增
     */
    @RequestMapping("/save")
    public String save(HttpServletRequest request,WwdxBannerModel model) {
        this.execute(request,model);
        model.setGmt_created(new Date());
        model.setGmt_modified(new Date());
        wwdxBannerService.save(model);
        String homeBannerList = ConstantHolder.HOME_BANNER_LIST;
        redisTemplate.delete(homeBannerList);
        return "redirect:/system/wwdx_banner/list";
    }

    private void execute(HttpServletRequest request, WwdxBannerModel bannerModel){
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
        String imgFile = "ImgFile";
        fileMap.forEach((k,file)->{
            if (file.getSize() > 0) {
                if (imgFile.equalsIgnoreCase(file.getName())) {
                    bannerModel.setImg(super.upload(file, directory + ConstantHolder.Constant.COVER));
                }
            }
        });
    }

    /**
     * 修改
     */
    @RequestMapping("/edit")
    public ModelAndView edit(Long bannerid) {
        ModelAndView mav = new ModelAndView();
        mav.addObject("model", wwdxBannerService.getById(bannerid));
        return mav;
    }

    /**
     * 保存修改
     */
    @RequestMapping("/update")
    public Object update(HttpServletRequest request, WwdxBannerModel bannerModel) {
        execute(request,bannerModel);
        wwdxBannerService.update(bannerModel);
        String homeBannerList = ConstantHolder.HOME_BANNER_LIST;
        redisTemplate.delete(homeBannerList);
        return "redirect:/system/wwdx_banner/list";
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresRoles("super")
    public Object delete(Long bannerid) {
        wwdxBannerService.delete(wwdxBannerService.getById(bannerid));
        return "redirect:/system/wwdx_banner/list";
    }
}